package com.zelma.admin.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zelma.admin.util.JwtUtil;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.zelma.admin.entity.ManagerEntity;
import com.zelma.admin.service.ManagerService;
import com.zelma.common.utils.PageUtils;
import com.zelma.common.utils.R;



/**
 * 
 *
 * @author zelma
 * @email 1239115389@qq.com
 * @date 2022-01-06 16:54:29
 */
@RestController
@RequestMapping("admin/manager")
@CrossOrigin
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    JwtUtil jwtUtil;

    //管理员页面
    @GetMapping("/list")
    public List<ManagerEntity> list(@RequestParam Map<String, Object> params) {
        return managerService.list();
    }

    //经纪人登录接口
    @PostMapping("/Login")
    public R login(@RequestBody ManagerEntity managerEntity) {
        ManagerEntity managerName = managerService.getOne(new QueryWrapper<ManagerEntity>().eq("manager_name", managerEntity.getManagerName()));
        if (managerName != null && managerEntity.getManagerPwd().equals(managerName.getManagerPwd())) {
            String token = jwtUtil.createJWT(managerEntity.getManagerName(), managerEntity.getManagerName(), "manager");
            redisTemplate.opsForValue().set("manager", managerEntity.getManagerName());
            return R.ok("登陆成功").put("token", token);
        }
        return R.error(404, "不存在该用户");
    }

}
